package vip.xiaonuo.biz.modular.lancerGoodsBox.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.lancerGoodsBox.service.BizGoodsBoxService;
import vip.xiaonuo.biz.modular.pojo.domain.BizGoods;
import vip.xiaonuo.biz.modular.pojo.domain.BizGoodsBox;
import vip.xiaonuo.biz.modular.pojo.dto.add.GoodsBoxAddDTO;
import vip.xiaonuo.biz.modular.pojo.dto.common.CommonDeleteDTO;
import vip.xiaonuo.biz.modular.pojo.dto.common.CommonDetailDTO;
import vip.xiaonuo.biz.modular.pojo.dto.edit.GoodsBoxEditDTO;
import vip.xiaonuo.biz.modular.pojo.dto.page.GoodsBoxPageDTO;
import vip.xiaonuo.biz.modular.pojo.vo.LV;
import vip.xiaonuo.common.pojo.CommonResult;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "商品盒子(B端)")
@ApiSupport(author = "LANCER_TEAM", order = 1)
@Validated

@RestController("userBusGoodsBoxController")
@RequestMapping
public class BusGoodsBoxController {
    @Autowired
    private BizGoodsBoxService GoodsBoxService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("B端商品盒子分页")
    @GetMapping("/biz/business/goodsBox/page")
    @SaCheckPermission("/biz/business/goodsBox/page")
    public CommonResult<Page<BizGoodsBox>> page(@Valid GoodsBoxPageDTO dto) {
        Page<BizGoodsBox> res = GoodsBoxService.pageBus(dto);
        return CommonResult.data(res);
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation("B端获取所有商品盒子")
    @GetMapping("/biz/business/goodsBox/allGoodsBox")
    @SaCheckPermission("/biz/business/goodsBox/allGoodsBox")
    public CommonResult<List<LV>> allGoodsBox() {
        List<BizGoodsBox> res = GoodsBoxService.allGoodsBox();
        if (ObjectUtil.isNotEmpty(res)) {
            List<LV> collect = res.stream().map(x -> {
                LV lv = new LV();
                lv.setLabel(x.getGoodsBoxName());
                lv.setValue(x.getId());
                return lv;
            }).collect(Collectors.toList());
            return CommonResult.data(collect);
        }
        return CommonResult.data(new ArrayList<>());
    }
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取盒子下的所有商品")
    @GetMapping("/biz/business/goodsBox/currBoxGoods/{goodsBoxId}")
    @SaCheckPermission("/biz/business/goodsBox/currBoxGoods/{goodsBoxId}")
    public CommonResult<List<LV>>  currBoxGoods(@PathVariable String goodsBoxId) {
        List<BizGoods> res = GoodsBoxService.currBoxGoods(goodsBoxId);
        if (ObjectUtil.isNotEmpty(res)) {
            List<LV> collect = res.stream().map(x -> {
                LV lv = new LV();
                lv.setLabel(x.getGoodsName());
                lv.setValue(x.getId());
                return lv;
            }).collect(Collectors.toList());
            return CommonResult.data(collect);
        }
        return CommonResult.data(new ArrayList<>());
    }
    @ApiOperationSupport(order = 2)
    @ApiOperation("B端商品盒子详情查询")
    @GetMapping("/biz/business/goodsBox/detail")
    @SaCheckPermission("/biz/business/goodsBox/detail")

    public CommonResult<BizGoodsBox> detail(@Valid CommonDetailDTO dto) {
        BizGoodsBox res = GoodsBoxService.detail(dto);
        return CommonResult.data(res);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("B端商品盒子新增")
    @PostMapping("/biz/business/goodsBox/add")
    @SaCheckPermission("/biz/business/goodsBox/add")

    public CommonResult<String> add(@Valid GoodsBoxAddDTO dto) {
        GoodsBoxService.add(dto);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("B端商品盒子删除")
    @PostMapping("/biz/business/goodsBox/delete")
    @SaCheckPermission("/biz/business/goodsBox/delete")

    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "删除id不能为空") List<CommonDeleteDTO> dto) {
        GoodsBoxService.delete(dto);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("B端商品盒子编辑")
    @PostMapping("/biz/business/goodsBox/edit")
    @SaCheckPermission("/biz/business/goodsBox/edit")
    public CommonResult<String> edit(@Valid GoodsBoxEditDTO dto) {
        GoodsBoxService.edit(dto);
        return CommonResult.ok();
    }
}